using System;
using System.Collections.Generic;
using System.Text;
namespace Test {
	class Stat {
		public List<KeyValuePair<string,int>> Stats {
			get {
				List<KeyValuePair<string,int>> lst=new List<KeyValuePair<string,int>>(_stats);
				lst.Sort(delegate(KeyValuePair<string,int> p1,KeyValuePair<string,int> p2) { return p2.Value.CompareTo(p1.Value); });
				return lst;
			}
		}

		public string Csv {
			get {
				StringBuilder sb=new StringBuilder();
				foreach(KeyValuePair<string,int> pair in Stats)
					sb.AppendLine(pair.Key+","+pair.Value);
				return sb.ToString();
			}
		}
		Dictionary<string,int> _stats=new Dictionary<string,int>();
		public virtual int Add(string key) {
			return Add(key,1);
		}
		public virtual int Add(string key,int cnt) {
			if(_stats.ContainsKey(key))
				_stats[key]+=cnt;
			else
				_stats.Add(key,cnt);
			return _stats[key];
		}
	}
	class DbStat:Stat {
		string _TblName;
		public Dictionary<string,int> Ids { get { return _ids; } }
		Dictionary<string,int> _ids=new Dictionary<string,int>();
		int idCnt=0;
		public DbStat(string TblName) { _TblName=TblName; }
		public override int Add(string key) {
			base.Add(key);
			if(!_ids.ContainsKey(key))
				_ids.Add(key,idCnt++);
			return _ids[key];
		}
	}



}
